package test.org.hxzon.util.stringparser;

import java.util.Collection;
import java.util.Map;

public class SceneBoutsInfo {
    private static final int MaxBout = 101;
    private SceneBoutInfo[] boutsInfo = new SceneBoutInfo[MaxBout];
    private SceneBoutInfo[] sectionsEnd = new SceneBoutInfo[MaxBout];
    private SceneBoutInfo[] sectionsStart = new SceneBoutInfo[MaxBout];
    private int size;
    private int maxSection;

    // ===========
    private static final int MaxDiff = 10;
    private SceneBoutsInfo header = null;
    // 注意，以下信息，只在第一个难度中维护
    private SceneBoutsInfo[] diffsInfo;
    private int diffSize;// 副本难度数
    private int[] tilis;// 不同难度所需的体力
    private int[] insLvls;// 副本等级

    // =============
    public void addBout(SceneBoutInfo boutInfo) {
        size++;
        boutsInfo[boutInfo.getBoutNo()] = boutInfo;
        int section = boutInfo.getSectionNo();
        maxSection = Math.max(maxSection, section);
        if (sectionsEnd[section] == null //
                || sectionsEnd[section].getBoutNo() < boutInfo.getSectionNo()) {
            sectionsEnd[section] = boutInfo;
        }
        if (sectionsStart[section] == null//
                || sectionsStart[section].getBoutNo() > boutInfo.getSectionNo()) {
            sectionsStart[section] = boutInfo;
        }
    }

    public SceneBoutInfo getBout(int i) {
        if (i < 1 || i > size) {
            return null;
        }
        return boutsInfo[i];
    }

    public SceneBoutInfo getSectionEnd(int i) {
        if (i < 1 || i > maxSection) {
            return null;
        }
        return sectionsEnd[i];
    }

    public SceneBoutInfo getSectionStart(int i) {
        if (i < 1 || i > maxSection) {
            return null;
        }
        return sectionsStart[i];
    }

    public int getMaxBoutNo() {
        return size;
    }

    public int getMaxSection() {
        return maxSection;
    }

    public void getMonstersId(Collection<String> output) {
        for (int i = 1; i <= size; i++) {
            for (Map<String, String> m : getBout(i).getMonsters()) {
                output.add(m.get("id"));
            }
        }
    }

    // ================
    public SceneBoutsInfo() {
        header = this;
        diffsInfo = new SceneBoutsInfo[MaxDiff];
        diffsInfo[diffSize] = this;
        diffSize++;
        tilis = new int[MaxDiff];// 不同难度所需的体力
        insLvls = new int[MaxDiff];// 副本等级
    }

    private SceneBoutsInfo(SceneBoutsInfo pre) {
        header = pre.header;
    }

    public SceneBoutsInfo newBoutsInfo() {
        header.diffSize++;
        SceneBoutsInfo newBoutsInfo = new SceneBoutsInfo(this);
        //
        header.diffsInfo[header.diffSize - 1] = newBoutsInfo;
        return newBoutsInfo;
    }

    public SceneBoutsInfo getBoutsInfo(int diff) {
        if (diff < 1 || diff > header.diffSize) {
            return null;
        }
        return header.diffsInfo[diff - 1];
    }

    public int getDiffSize() {
        return header.diffSize;
    }

    public int getTili(int diff) {
        if (diff < 1 || diff > header.diffSize) {
            return 0;
        }
        return header.tilis[diff - 1];
    }

    public int getInsLvl(int diff) {
        if (diff < 1 || diff > header.diffSize) {
            return 0;
        }
        return header.insLvls[diff - 1];
    }

    public void setTili(int diff, int tl) {
        if (diff < 1 || diff > MaxDiff) {
            return;
        }
        header.tilis[diff - 1] = tl;
    }

    public void setInsLvl(int diff, int lvl) {
        if (diff < 1 || diff > MaxDiff) {
            return;
        }
        header.insLvls[diff - 1] = lvl;
    }
}
